﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.IO;
using Microsoft.Win32;

namespace WpfClient.Content
{
    /// <summary>
    /// TextBoxWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TextBoxWindow : Window
    {
        TextBox txt;

        RichTextBox txtBox;

        string strFilter = "Document Files(*.xaml)|*.xaml|All Files(*.*)|*.*";
        public TextBoxWindow()
        {
            InitializeComponent();

            //txt = new TextBox();
            //txt.AcceptsReturn = true;
            //txt.TextWrapping = TextWrapping.Wrap;
            //txt.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

            //this.Content = txt;

            txtBox = new RichTextBox();
            txtBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

            this.Content = txtBox;

            txtBox.Focus();

        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            //base.OnPreviewTextInput(e);

            if (e.ControlText.Length > 0 && e.ControlText[0] == '\x0f')
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.CheckFileExists = true;
                dlg.Filter = strFilter;

                if ((bool)dlg.ShowDialog(this))
                {
                    FlowDocument doc = txtBox.Document;
                    TextRange txtRange = new TextRange(doc.ContentStart, doc.ContentEnd);

                    Stream stream = null;
                    try
                    {
                        stream = new FileStream(dlg.FileName, FileMode.Open);
                        txtRange.Load(stream, DataFormats.Text);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);

                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Close();
                        }
                    }
                    e.Handled = true;
                }
            }

            if (e.ControlText.Length > 0 && e.ControlText[0] == '\x0f')
            {

            }

            base.OnPreviewTextInput(e);

        }
    }
}
